package com.zbcz.crm

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import com.zbcz.crm.databinding.ActivityMainBinding
import com.zbcz.crm.sp.SharedPreferencesManager
import com.zbcz.crm.util.IS_TEST_MODE
import com.zbcz.crm.util.adjustLayoutForStatusBar
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.viewmodel.LoginViewModel

class MainActivity : BaseActivity() {

    private lateinit var mBinding: ActivityMainBinding

    private val mLoginViewModel: LoginViewModel by viewModels()

    companion object {
        const val TAG = "MainActivity"

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        init()
    }


    private var isPasswordVisible = false
    private fun init() {
        //adjustLayoutForStatusBar(this,mBinding.homeWrapper)
        mBinding.eyeIcon.onSingleClick {
            isPasswordVisible = !isPasswordVisible
            if (isPasswordVisible) {
                // 如果密码可见，则设置输入类型为明文
                mBinding.edPasswordInput.transformationMethod = null
                mBinding.eyeIcon.setImageResource(R.drawable.open_eye)
            } else {
                // 如果密码隐藏，则设置输入类型为密码
                mBinding.edPasswordInput.transformationMethod =
                    android.text.method.PasswordTransformationMethod.getInstance()
                mBinding.eyeIcon.setImageResource(R.drawable.close_eye)
            }
            // 将光标移动到末尾
            mBinding.edPasswordInput.setSelection(mBinding.edPasswordInput.text.length)
        }
        mBinding.tvLoginButton.onSingleClick {
            val account = mBinding.edAccount.text.toString().trim()
            val password = mBinding.edPasswordInput.text.toString().trim()

            if (account.isEmpty()) {
                showToast("请输入账户名")
                return@onSingleClick
            }

            if (password.isEmpty() || password.length < 6) {
                showToast("密码应至少为6个字符")
                return@onSingleClick
            }
            mLoginViewModel.login(account, password)
        }
        mLoginViewModel.mLoginResponse.observe(this) { userInfo ->
            if (userInfo != null) {
                SharedPreferencesManager.saveUserInfo(this@MainActivity, userInfo)
                Log.i(TAG, userInfo.toString())
                showToast("登陆成功")
                startActivity(Intent(this@MainActivity, HomeActivity::class.java))
                finish()
            }
        }
        initTest()
    }

    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }

    private fun initTest() {
        if (IS_TEST_MODE) {
            mBinding.edAccount.setText("admin")
            mBinding.edPasswordInput.setText("123456")
        }
    }
}